package alex.rsx.view;

import alex.rsx.activity.r.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

public class ExpertDrawer extends ViewGroup {

	private final String TAG = "ExpertDrawer";
	private final int RIGHT_OPEN = 0x01;
	private final int RIGHT_CLOSE = 0x02;
	private final int LEFT_OPEN = 0x03;
	private final int LEFT_CLOSE = 0x04;
	private int mLastType;
	
	
	private Scroller mScroller;
	private Rect mContentFrame;
	private boolean mIsBeingDragged = false;
	
	private float mDownX;
	private float mUpX;
	private float mContentOffX;
	private int mDragDistance;
	private int mLeftEnd;
	private int mRightStart;
	
	private int mRightViewId = 0;
	private int mLeftViewId = 0;
	private int mContentViewId = 0;
	
	private View mRightView;
	private View mLeftView;
	private View mContentView;
	private View mLeftShadow;
	private View mRightShadow;
	private final int mShadow = 15;
	
	
	public ExpertDrawer(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
		
	}

	public ExpertDrawer(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.slidingDrawer, defStyle, 0);
		mRightViewId = a.getResourceId(R.styleable.slidingDrawer_rightViewId, 0);
		mLeftViewId = a.getResourceId(R.styleable.slidingDrawer_leftViewId, 0);
		mContentViewId = a.getResourceId(R.styleable.slidingDrawer_contentViewId, 0);

		a.recycle();

		setAlwaysDrawnWithCacheEnabled(false);
		mContentOffX = 0;
		mScroller = new Scroller(context);
		mContentFrame= new Rect(0,0,this.getWidth(),this.getHeight());
		mLeftShadow = new View(context);
		GradientDrawable mBackShadowDrawableLR = new GradientDrawable(
				GradientDrawable.Orientation.LEFT_RIGHT, new int[] { 0x00000000, 0xff000000 });
		mBackShadowDrawableLR.setGradientType(GradientDrawable.LINEAR_GRADIENT);
		mBackShadowDrawableLR.setBounds(0, 0, mShadow, this.getBottom());
		
		mLeftShadow.setBackgroundDrawable(mBackShadowDrawableLR);
		
		mRightShadow= new View(context);
		GradientDrawable mBackShadowDrawableRL = new GradientDrawable(
				GradientDrawable.Orientation.LEFT_RIGHT, new int[] { 0xff000000, 0x00000000 });
		mBackShadowDrawableRL.setGradientType(GradientDrawable.LINEAR_GRADIENT);
		mBackShadowDrawableRL.setBounds(0, 0, mShadow, this.getBottom());
		mRightShadow.setBackgroundDrawable(mBackShadowDrawableRL);
		
	}
	
	

	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		mContentView = findViewById(mContentViewId);
		mContentView.setClickable(true);
		mLeftView = findViewById(mLeftViewId);
		mRightView = findViewById(mRightViewId);
	}
	int rightMarg;
	int leftMarg;
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		
		mLeftEnd = mLeftView.getMeasuredWidth();
		mRightStart = r - mRightView.getMeasuredWidth();
		Log.e(TAG, "onLayout x:"+mContentOffX);
		
		mLeftView.layout(0+leftMarg, t, mLeftEnd, b);
		mRightView.layout(mRightStart+ rightMarg, t, r, b);
		mLeftShadow.layout((int)mContentOffX-mShadow, t, (int)mContentOffX, b);
		mRightShadow.layout(r+(int)mContentOffX, t, r+(int)mContentOffX+mShadow, b);
		mContentView.layout((int)mContentOffX, t, r+(int)mContentOffX, b);
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		// TODO Auto-generated method stub

		final long drawingTime = getDrawingTime();
		drawChild(canvas, mLeftView, drawingTime);
		drawChild(canvas, mRightView, drawingTime);
		final Bitmap cache = mContentView.getDrawingCache();
		
		if (cache != null) {
			canvas.drawBitmap(cache, 0, mContentView.getBottom(), null);
		} else {
			canvas.save();
			drawChild(canvas, mContentView, drawingTime);
			canvas.restore();
		}
		drawChild(canvas, mLeftShadow, drawingTime);
		drawChild(canvas, mRightShadow, drawingTime);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		measureChild(mLeftView, widthMeasureSpec, heightMeasureSpec);
		measureChild(mRightView, widthMeasureSpec, heightMeasureSpec);
		measureChild(mContentView, widthMeasureSpec, heightMeasureSpec);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent event) {
		
		if(mContentFrame.contains((int)event.getX(),(int)event.getY())){
			if(mLastType == LEFT_OPEN){
				startAnimation(400,LEFT_CLOSE,true);
				return true;
			}else if(mLastType == RIGHT_OPEN){
				startAnimation(400,RIGHT_CLOSE,true);
				return true;
			}
      	}
		
		int x = (int) event.getX();
		final int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			mDownX = event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			if (Math.abs(x - mDownX) > 50) {
				mIsBeingDragged = true;
			}
			break;
		case MotionEvent.ACTION_UP:
			mIsBeingDragged = false;
			mDownX = 0;
			break;

		default:
			break;
		}
		return mIsBeingDragged;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
			final int action = event.getAction();
			switch (action) {
			
			case MotionEvent.ACTION_DOWN:
				break;
			case MotionEvent.ACTION_MOVE:
				startX = (int)event.getX();
				if(mLastType == LEFT_CLOSE || mLastType == RIGHT_CLOSE){
					//moveHandle((int) event.getX());
				}
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				mUpX = event.getX();
				mIsBeingDragged = false;
				
				if(mDownX - mUpX>50){
					//向左滑
					if(mLastType == LEFT_OPEN){
					}else{
						startAnimation(400,RIGHT_OPEN,true);
					}
				} else if(-50>mDownX - mUpX){
					//向右滑
					if(mLastType == RIGHT_OPEN){
					}else{
						startAnimation(400,LEFT_OPEN,true);
					}
				}
				mDownX = 0;
				break;
			}
		return  super.onTouchEvent(event);
	}

	private void moveHandle(int position) {
		mContentOffX = (int) (position - mDownX);
		if (mLeftEnd < mContentOffX) {
			mContentOffX = mLeftEnd;
		} else if (mContentOffX < (mRightStart - mContentView.getRight())) {
			mContentOffX = mRightStart - mContentView.getRight();
		}
		postInvalidate();
	}

	@Override
	public void computeScroll() {
		// TODO Auto-generated method stub
		Log.e(TAG, "computeScroll x:"+mContentOffX);
		
		if(mScroller.computeScrollOffset()){
			float x = mScroller.getCurrX();
			mContentOffX =  x;
			postInvalidate();
			requestLayout();
		}
			
		super.computeScroll();
	}

	private int startX;
	
	private void startAnimation(int delayMillis, int type,boolean isFull) {
		
		abortAnimation();
		postInvalidate();
		mLastType = type;
		int dx = 0;
		switch (type) {
		case RIGHT_OPEN:
			rightMarg = 0;
			leftMarg = - (this.getWidth() -mRightStart); 
			mContentFrame = new Rect(0, 0, mRightView.getLeft(), this.getBottom());
			if(isFull){
				dx =-(this.getWidth()-mRightStart);
				startX= 0;
			}else{
				dx =-(this.getWidth()-mRightStart-(this.getWidth()-startX) ) ;
				startX = -(this.getWidth()-startX);
			}
			break;
		case RIGHT_CLOSE:
			mContentFrame = new Rect(0, 0, 0, 0);
			if(isFull){
				startX = mRightStart - this.getWidth();
				dx = this.getWidth() - mRightStart;
			}else{
				dx =startX - this.getWidth();
				startX = startX - mRightView.getRight();
			}
			break;
		case LEFT_OPEN:
			rightMarg = mLeftEnd;
			leftMarg = 0;
			mContentFrame = new Rect(mLeftEnd, 0, this.getRight(), this.getBottom());
			if(isFull){
				startX = 0;
				dx = mLeftEnd;
			}else{
				dx = mLeftEnd - startX;
			}
			break;
		case LEFT_CLOSE:
			mContentFrame = new Rect(0, 0, 0, 0);
			if(isFull){
				startX = mLeftEnd;
				dx = -mLeftEnd;
			}else{
				dx = 0-startX;
			}
			break;
		default:
			break;
		}
		
		mScroller.startScroll(startX, 0, dx, 0,delayMillis);
	}

	public void abortAnimation() {
		if (!mScroller.isFinished()) {
			mScroller.abortAnimation();
		}
	}
	
	public void openLeft(){
		startAnimation(400,LEFT_OPEN,true);
	}
	
	public void openRight(){
		startAnimation(400,RIGHT_OPEN,true);
	}
	
	public void closeLeft(){
		startAnimation(400,LEFT_CLOSE,true);
	}
	
	public void closeRight(){
		startAnimation(400,RIGHT_CLOSE,true);
	}
	
	public void setContentView(View view){
		ViewGroup content = ((ViewGroup)mContentView);
		content.removeAllViews();
		content.addView(view,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	}
	public void setLeftView(View view){
		ViewGroup left = ((ViewGroup)mLeftView);
		left.removeAllViews();
		left.addView(view);
		
	}
	public void setRightView(View view){
		ViewGroup right = ((ViewGroup)mRightView);
		right.removeAllViews();
		right.addView(view);
	}
	
}
